package widget;

import adapter.SieveHomePageAdapter;
import android.animation.ValueAnimator;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import adapter.SieveHomePageCanAdapter;
import base.BaseBean;
import bean.AddrIDBean;
import bean.AreaBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dispatchapp.andlei.R;

import bean.CanTeenBean;
import netWork.NetUtils;
import utils.GsonUtils;
import utils.SPUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * 筛选弹窗
 * Created by Andlei on 2018/7/23.
 */

public class SieveHomePagePopupWindow extends PopupWindow {
    private Context mContext;
    private final View mView;
    private LinearLayout mLlContent;
    private RecyclerView recyclerView_type;
    private RecyclerView recyclerView;
    private RecyclerView recyclerView_type_can;
    private SieveHomePageAdapter adapter;
    private SieveHomePageAdapter adapter_type;
    private SieveHomePageCanAdapter adapter_can;
    private List<AddrIDBean> list = new ArrayList<>();
    private List<CanTeenBean> list_can = new ArrayList<>();
    private List<AddrIDBean> list_type = new ArrayList<>();
    private NetUtils netUtils;
    private Button btn_reset, btn_sure;

    // private Activity mActivity;
    public SieveHomePagePopupWindow(Context context, View.OnClickListener onClickListener) {
        super(context);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.popupwindow_sievehomepage, null);
        netUtils = new NetUtils(mContext, netRequestCallBack);
        initView(mView);
        btn_reset.setOnClickListener(onClickListener);
        btn_sure.setOnClickListener(onClickListener);
    }

    private void initView(View view) {
        mLlContent = view.findViewById(R.id.pop_layout);
        this.setContentView(mView);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setAnimationStyle(R.style.AppTheme);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);

        mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        hiddenPopupWindow();
                    }
                }
                return true;
            }
        });
        recyclerView_type = mView.findViewById(R.id.list_rcy_);
        recyclerView = mView.findViewById(R.id.list_rcy);
        recyclerView_type_can = mView.findViewById(R.id.list_rcy_canting);
        btn_reset = mView.findViewById(R.id.btn_reset);
        btn_sure = mView.findViewById(R.id.btn_sure);
//        AddrIDBean addrIDBean0 =  new AddrIDBean();
        AddrIDBean addrIDBean = new AddrIDBean();
        AddrIDBean addrIDBean1 = new AddrIDBean();
//        addrIDBean0.setAddr_id(0);
//        addrIDBean0.setAddr_name("全部");
        addrIDBean.setAddr_id(2);
        addrIDBean.setAddr_name("校内校外");
        addrIDBean1.setAddr_id(3);
        addrIDBean1.setAddr_name("仅校内");
//        list_type.add(addrIDBean0);
        //distribution_type：0，全部   2，支持外端物流  3，不支持外端物流
        list_type.add(addrIDBean);
        list_type.add(addrIDBean1);
        String typeId = SPUtils.getInstance(mContext).getString("type_id");
        if (!TextUtils.isEmpty(typeId)) {
            if (typeId.contains("2")) {
                list_type.get(0).isSelect = true;

            }
            if (typeId.contains("3")) {
                list_type.get(1).isSelect = true;
            }
        }
        netUtils.get("rider/user/area-addr", BaseBean.class, false);
//        TestBean bean = new TestBean();
//        bean.str = "基础配送费";
//        bean.money = moneyListBean.getBase()+"";
//        TestBean bean1 = new TestBean();
//        bean1.str = "特殊奖励";
//        bean1.money = moneyListBean.getSpecial()+"";
//        TestBean bean2 = new TestBean();
//        bean2.str = "时段奖励";
//        bean2.money = moneyListBean.getTime()+"";
//        TestBean bean3 = new TestBean();
//        bean3.str = "等级奖励";
//        bean3.money = moneyListBean.getLevel()+"";
//        TestBean bean4 = new TestBean();
//        bean4.str = "距离奖励";
//        bean4.money = moneyListBean.getDistance()+"";
//        TestBean bean5 = new TestBean();
//        bean5.str = "任务奖励";
//        bean5.money = moneyListBean.getTask()+"";
//        list.add(bean);
//        list.add(bean1);
//        list.add(bean2);
//        list.add(bean3);
//        list.add(bean4);
//        list.add(bean5);
//        for (int i=0;i<10;i++){
//            AddrIDBean bean = new AddrIDBean();
//            bean.setAddr_id(""+i);
//            bean.setAddr_name("餐馆餐馆餐馆餐馆餐馆餐馆"+i);
//            list.add(bean);
//        }
        //配送类型
        GridLayoutManager layoutManager_type = new GridLayoutManager(mContext, 3);
        recyclerView_type.setLayoutManager(layoutManager_type);
        adapter_type = new SieveHomePageAdapter(R.layout.item_sieve_homepage, list_type);
        recyclerView_type.setAdapter(adapter_type);
        adapter_type.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                list_type.get(position).isSelect = !list_type.get(position).isSelect;
                for (int i = 0; i < list_type.size(); i++) {
                    if (list_type.get(i).isSelect) {

                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
        //餐厅
        GridLayoutManager layoutManager_can = new GridLayoutManager(mContext, 3);
        recyclerView_type_can.setLayoutManager(layoutManager_can);
        adapter_can = new SieveHomePageCanAdapter(R.layout.item_sieve_homepage, list_can);
        recyclerView_type_can.setAdapter(adapter_can);
        adapter_can.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                list_can.get(position).isSelect = !list_can.get(position).isSelect;
                for (int i = 0; i < list_can.size(); i++) {
                    if (list_can.get(i).isSelect) {

                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
        //区域
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new SieveHomePageAdapter(R.layout.item_sieve_homepage, list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if(position==0||position==1){
//                    if(position == 0){
//                        if (list.get(0).isSelect) {
//
//                            list.get(1).isSelect = false;
//                            list.get(0).isSelect = false;
//                        }else {
//                            list.get(0).isSelect = true;
//                            if(list.get(1).isSelect){
//                                list.get(1).isSelect = false;
//                            }
//                        }
//                    }else {
//                        if (list.get(1).isSelect) {
//                            list.get(0).isSelect = false;
//                            list.get(1).isSelect = false;
//                        }else {
//                            list.get(1).isSelect = true;
//                            if(list.get(0).isSelect){
//                                list.get(0).isSelect = false;
//                            }
//                        }
//                    }
//                }else {
                list.get(position).isSelect = !list.get(position).isSelect;
//                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    public void hiddenPopupWindow() {
        mView.clearAnimation();
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.slide_out);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        mView.startAnimation(animation);
        setAnimation(180, 0);

    }

    private void setAnimation(int start, int end) {
        mLlContent.clearAnimation();
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);

        valueAnimator.setDuration(300).start();
        valueAnimator.setTarget(mLlContent);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                getBackground().setAlpha((Integer) animation.getAnimatedValue());
            }
        });
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        mView.clearAnimation();
        mView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in));
        setAnimation(0, 180);
        super.showAtLocation(parent, gravity, x, y);
    }

    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
//            list.addAll(GsonUtils.getInstance(mContext).parseNoHeaderJArray(t, AddrIDBean.class));
//            adapter.setNewData(list);
            list = GsonUtils.getInstance(mContext).parseNoHeaderJArray(t, AddrIDBean.class);
            list_can = GsonUtils.getInstance(mContext).parseNoHeaderJArrayCan(t.getCanteen(), CanTeenBean.class) ;
            String AddrID = SPUtils.getInstance(mContext).getString("addr_id");
            if (!TextUtils.isEmpty(AddrID)) {
                String[] arr = AddrID.split(",");
                ArrayList<String> alist = new ArrayList<String>();
                Collections.addAll(alist, arr);
                ArrayList<String> slist = new ArrayList<>();
                for (AddrIDBean bean : list) {
                    slist.add(String.valueOf(bean.getAddr_id()));
                }
                for (String s : alist) {
                    if (slist.contains(s)) {
                       list.get(slist.indexOf(s)).isSelect = true;
                    }
                }
            }
            adapter.setNewData(list);
            adapter_can.setNewData(list_can);
        }
    };

    public String ReturnAddr_Id() {
        String addr_id = "";
        for (int i = 0; i < list.size(); i++) {

            if (list.get(i).isSelect()) {
                addr_id += list.get(i).getAddr_id() + ",";
            }

        }
        if (!TextUtils.isEmpty(addr_id)) {
            addr_id = addr_id.substring(0, addr_id.length() - 1);
        }
        return addr_id;
    }

    public String ReturnCan_Id() {
        String can_id = "";
        for (int i = 0; i < list_can.size(); i++) {

            if (list_can.get(i).isSelect) {
                can_id += list_can.get(i).getId() + ",";
            }

        }
        if (!TextUtils.isEmpty(can_id)) {
            can_id = can_id.substring(0, can_id.length() - 1);
        }
        return can_id;
    }

    public String ReturnType_Id() {
        String type_id = "";
        if (list_type.get(0).isSelect) {
            type_id = list_type.get(0).getAddr_id() + ",";
        }
        if (list_type.get(1).isSelect) {
            type_id = type_id + list_type.get(1).getAddr_id();
        }
        if(!TextUtils.isEmpty(type_id)){
            if(type_id.substring(type_id.length()-1,type_id.length()).equals(",")){
                type_id = type_id.substring(0,type_id.length()-1);
            }
        }
        return type_id;
    }

    public void resetAddr_Id() {
        for (AddrIDBean bean : list) {
            bean.isSelect = false;
        }
        for (CanTeenBean bean : list_can) {
            bean.isSelect = false;
        }
        adapter.notifyDataSetChanged();
        adapter_can.notifyDataSetChanged();
    }
}















